const {menu} = require("../models/menu");
const {role} = require("../models/mrole");
const {muser} = require("../models/muser");
const bcrypt = require("bcryptjs");

const initDatabase = async () => {
    let item = await menu.findOne({
        alias: "系统设置"
    })
    if (!item) {
        //初始化基本配置菜单
        menu.create({
            name: 'setting',
            path: 'setting',
            isMenu: true,
            disabled: false,
            order: 0,
            alias: "系统设置",
            parentId: "0",
        }).then(async (res) => {
            let arr = [{
                name: 'menu',
                path: 'menu',
                isMenu: false,
                disabled: false,
                order: 0,
                alias: "菜单管理",
                parentId: res._id,
            }, {
                name: 'role',
                path: 'role',
                isMenu: false,
                disabled: false,
                order: 1,
                alias: "职务管理",
                parentId: res._id,
            }, {
                name: 'user',
                path: 'user',
                isMenu: false,
                disabled: false,
                order: 2,
                alias: "用户管理",
                parentId: res._id,
            }]

            // 使用 Promise.all 等待所有子菜单创建完成
            await Promise.all(arr.map(menuItem => menu.create(menuItem)));

            console.log('菜单初始化完成')


            //初始化运维带师
            let mrole = await role.findOne({
                name: '运维带师'
            })

            if (!mrole) {
                menu.find({}).then(res => {
                    role.create({
                        name: '运维带师',
                        roles: res.map(e => e._id)
                    }).then(async res => {
                        console.log('职务初始化完成')
                        let user = await muser.findOne({
                            username: '17516211205'
                        })

                        if (!user) {
                            //初始化用户17516211205
                            let password = await bcrypt.hash('111', await bcrypt.genSalt(10));
                            muser.create({
                                username: '17516211205',
                                roles: [res._id],
                                password: password,
                                phone: '',
                                email: '',
                            }).then(res => {
                                console.log('用户初始化完成')

                            })
                        }

                    })


                })


            }


        })
    }


    let item1 = await menu.findOne({
        alias: "客户账单设置"
    })

    if (!item1) {
        menu.create({
            name: 'bill',
            path: 'bill',
            isMenu: true,
            disabled: false,
            order: 0,
            alias: "客户账单设置",
            parentId: "0",
        }).then(async (res) => {
            let arr = [{
                name: 'billlist',
                path: 'billlist',
                isMenu: false,
                disabled: false,
                order: 0,
                alias: "客户账单管理",
                parentId: res._id,
            },
                {
                    name: 'productlist',
                    path: 'productlist',
                    isMenu: false,
                    disabled: false,
                    order: 1,
                    alias: "客户产品管理",
                    parentId: res._id,
                }
            ]
            // 使用 Promise.all 等待所有子菜单创建完成
            await Promise.all(arr.map(menuItem => menu.create(menuItem)));
            console.log('客户账单管理初始化完成')


            let billitem = await menu.findOne({
                alias: "客户账单管理"
            })
            if (billitem) {
                let btnarry = [{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'add',
                    name: '新增',
                },{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'dataview',
                    name: '大屏数据',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'delete',
                    name: '删除',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'put',
                    name: '修改',
                },{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'sum',
                    name: '计算',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'search',
                    name: '查询',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'export',
                    name: '导出',
                }]
                await Promise.all(btnarry.map(Item => menu.findOneAndUpdate({_id: billitem._id}, {
                    $push: {
                        btnRoles: Item
                    }
                })));
                console.log('客户账单管理按钮初始化完成')
            }
            let billitem1 = await menu.findOne({
                alias: "客户产品管理"
            })
            if (billitem1) {
                let btnarry = [{
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'add',
                    name: '新增',
                },{
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'sum',
                    name: '计算',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'delete',
                    name: '删除',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'put',
                    name: '修改',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'search',
                    name: '查询',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'export',
                    name: '导出',
                }]
                await Promise.all(btnarry.map(Item => menu.findOneAndUpdate({_id: billitem1._id}, {
                    $push: {
                        btnRoles: Item
                    }
                })));
                console.log('供应商产品管理按钮初始化完成')
            }




        })




    }
    let item2 = await menu.findOne({
        alias: "供应商账单设置"
    })

    if (!item2) {
        menu.create({
            name: 'suppliers',
            path: 'suppliers',
            isMenu: true,
            disabled: false,
            order: 1,
            alias: "供应商账单设置",
            parentId: "0",
        }).then(async (res) => {
            let arr = [{
                name: 'suppliersProductList',
                path: 'suppliersProductList',
                isMenu: false,
                disabled: false,
                order: 0,
                alias: "供应商产品管理",
                parentId: res._id,
            }, {
                name: 'supplierslist',
                path: 'supplierslist',
                isMenu: false,
                disabled: false,
                order: 1,
                alias: "供应商账单管理",
                parentId: res._id,
            }]
            // 使用 Promise.all 等待所有子菜单创建完成
            await Promise.all(arr.map(menuItem => menu.create(menuItem)));
            console.log('供应商账单管理初始化完成')


            let billitem = await menu.findOne({
                alias: "供应商账单管理"
            })
            if (billitem) {
                let btnarry = [{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'add',
                    name: '新增',
                },{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'sum',
                    name: '计算',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'delete',
                    name: '删除',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'put',
                    name: '修改',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'search',
                    name: '查询',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'export',
                    name: '导出',
                }]
                await Promise.all(btnarry.map(Item => menu.findOneAndUpdate({_id: billitem._id}, {
                    $push: {
                        btnRoles: Item
                    }
                })));
                console.log('供应商账单管理按钮初始化完成')
            }


            let billitem1 = await menu.findOne({
                alias: "供应商产品管理"
            })
            if (billitem1) {
                let btnarry = [{
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'add',
                    name: '新增',
                },{
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'sum',
                    name: '计算',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'delete',
                    name: '删除',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'put',
                    name: '修改',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'search',
                    name: '查询',
                }, {
                    menuId: billitem1._id,
                    color: '#79bbff',
                    code: 'export',
                    name: '导出',
                }]
                await Promise.all(btnarry.map(Item => menu.findOneAndUpdate({_id: billitem1._id}, {
                    $push: {
                        btnRoles: Item
                    }
                })));
                console.log('供应商产品管理按钮初始化完成')
            }


        })
    }
    let item3 = await menu.findOne({
        alias: "订单关联设置"
    })

    if (!item3) {
        menu.create({
            name: 'order',
            path: 'order',
            isMenu: true,
            disabled: false,
            order: 2,
            alias: "订单关联设置",
            parentId: "0",
        }).then(async (res) => {
            let arr = [{
                name: 'orderlist',
                path: 'orderlist',
                isMenu: false,
                disabled: false,
                order: 0,
                alias: "订单关联管理",
                parentId: res._id,
            }]
            // 使用 Promise.all 等待所有子菜单创建完成
            await Promise.all(arr.map(menuItem => menu.create(menuItem)));
            console.log('订单关联初始化完成')

            let billitem = await menu.findOne({
                alias: "订单关联管理"
            })
            if (billitem) {
                let btnarry = [{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'add',
                    name: '新增',
                },{
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'sum',
                    name: '计算',
                },  {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'delete',
                    name: '删除',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'put',
                    name: '修改',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'search',
                    name: '查询',
                }, {
                    menuId: billitem._id,
                    color: '#79bbff',
                    code: 'export',
                    name: '导出',
                }]
                await Promise.all(btnarry.map(Item => menu.findOneAndUpdate({_id: billitem._id}, {
                    $push: {
                        btnRoles: Item
                    }
                })));
                console.log('订单关联管理按钮初始化完成')
            }
        })
    }
}
module.exports = {
    initDatabase
}
